using Eno.Application.Services;
using Eno.Infrastructure.Tools;
using Microsoft.AspNetCore.Mvc;

namespace Eno.Api.Controllers;

[ApiController]
[Route("api/[controller]")]
public class LogController : ControllerBase
{
    private readonly ILogService _logService;

    public LogController(ILogService logService)
    {
        _logService = logService;
    }

    [HttpGet]
    public async Task<IActionResult> GetLogs([FromQuery] int pageNumber = 1, [FromQuery] int pageSize = 50, [FromQuery] string? level = null)
    {
        try
        {
            var logs = await _logService.GetLogsAsync(pageNumber, pageSize, level);
            return Ok(Tips<object>.Success(new { Logs = logs, PageNumber = pageNumber, PageSize = pageSize, Level = level }, "获取日志成功"));
        }
        catch (Exception ex)
        {
            await _logService.LogErrorAsync("获取日志失败", ex, "LogController");
            return Ok(Tips<object>.Fail("获取日志失败"));
        }
    }

    [HttpGet("{id}")]
    public async Task<IActionResult> GetLogById(int id)
    {
        try
        {
            var log = await _logService.GetLogByIdAsync(id);
            if (log == null)
            {
                return Ok(Tips<object>.Fail("日志不存在"));
            }
            
            return Ok(Tips<object>.Success(log, "获取日志详情成功"));
        }
        catch (Exception ex)
        {
            await _logService.LogErrorAsync("获取日志详情失败", ex, "LogController", new { LogId = id });
            return Ok(Tips<object>.Fail("获取日志详情失败"));
        }
    }
} 